/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Seoprofesional
 *
 * The Initial Developer of the Original Code is
 * Petr Blahoš.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

var links_count = 0;
var links_inpage = 0;
var links_nofollow = 0;
var word_count = 0;

onLoadRegistry.push(function() {
	var el = document.getElementById("seopTab");
	el.hidden = false;
});

onProcessFrame.push(function(doc) {
	var uri = doc.baseURIObject.spec;
	var domain = SeoprofesionalTools.get_domain(uri);

	links_count+= doc.links.length;

	for (var i = 0; i<doc.links.length;i++) {
		var hr = doc.links[i].href;
		var hash = hr.indexOf("#");
		if (SeoprofesionalTools.get_domain(hr)==domain) {
			links_inpage++;
		}
		if (-1!=doc.links[i].rel.toLowerCase().indexOf("nofollow")) {
			links_nofollow++;
		}
	}

	var words = doc.evaluate("normalize-space(/*)", doc, null, XPathResult.STRING_TYPE, null).stringValue.toLowerCase()
			.split(/[ \~\t\r\n!@#\$%\^\&\*\.\+\(\)\?\!\[\]\/"',;:{}=<>-]+/);

	var x = words_view.getData();
	for (var i = 0;i<words.length;i++) {
		if (' '==words[i] || ''==words[i] || '|'==words[i]) {
			continue;
		}
		word_count++;
		if (undefined==x[words[i]]) {
			x[words[i]] = 1;
		} else {
			x[words[i]]++;
		}
	}
	var q = doc.evaluate("//script", doc, null, XPathResult.ANY_TYPE, null);
	var t = q.iterateNext();
	while (t) {
		words = t.textContent.toLowerCase().split(/[ \~\t\r\n!@#\$%\^\&\*\.\+\(\)\?\!\[\]\/\|"',;:{}=<>-]+/);
		for (var i = 0;i<words.length;i++) {
			if (' '==words[i] || ''==words[i] || '|'==words[i]) {
				continue;
			}
			word_count--;
			if (undefined!=x[words[i]]) {
				x[words[i]]--;
			}
		}
		t = q.iterateNext();
	}
	words_view.setData(x);

	document.getElementById("linkcount").value = links_count;
	document.getElementById("linksinpage").value = links_inpage;
	document.getElementById("linksnofollow").value = links_nofollow;
	document.getElementById("linksexternal").value = links_count - links_inpage;

	document.getElementById("wordcount").value = word_count;
});

var words_view = {
data_src: new Object(),
data: [],
last_key: "",
init: function() {
	words_view.treeBox = null;
},
sort: function(key) {
	if (words_view.last_key==key) {
		var el = document.getElementById(key)
		if ("ascending"==el.getAttribute("sortDirection")) {
			el.setAttribute("sortDirection", "descending");
		} else {
			el.setAttribute("sortDirection", "ascending");
		}
		words_view.data.reverse();
	} else {
		if ("word"==key) {
			words_view.data.sort();
			document.getElementById("count").setAttribute("sortDirection", "none");
			document.getElementById("word").setAttribute("sortDirection", "descending");
		} else {
			words_view.data.sort(function(x1, x2){ return x2[0]-x1[0]; });
			document.getElementById("count").setAttribute("sortDirection", "ascending");
			document.getElementById("word").setAttribute("sortDirection", "none");
		}
		words_view.last_key = key;
	}
	if (null!=words_view.treeBox) {
		words_view.treeBox.invalidate();
	}
},
copy: function() {
	var out = "";
	for (var i = 0;i<words_view.data.length;i++) {
		out+= words_view.data[i][1] + "," + words_view.data[i][0] + "\r\n";
	}
	Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper).copyString(out);
},
getData: function() {	return words_view.data_src;	},
setData: function(data) {
	if (null!=words_view.treeBox) {
		words_view.treeBox.rowCountChanged(0, -words_view.data.length);  
	}
	words_view.data_src = data;
	var out = new Array();
	for (var i in data) {
		if (data[i]>0) {
			out.push([ data[i], i ])
		}
	}
	words_view.sort("count");
	out.sort(function(x1,x2) {
		return x2[0]-x1[0];
	});
	words_view.data = out;
	if (null!=words_view.treeBox) {
		words_view.treeBox.rowCountChanged(0, words_view.data.length);  
	}
},
treeBox: null,  
get rowCount()                     { return words_view.data.length; },  
setTree: function(treeBox)	   {
	this.treeBox = treeBox;
}, 
getCellText: function(idx, column) {
	if ("word"==column.id) {
		return words_view.data[idx][1];
	}
	return ""+words_view.data[idx][0];
},  
isContainer: function(idx)         { return false; },  
isContainerOpen: function(idx)     { return false; },  
isContainerEmpty: function(idx)    { return false; },  
isSeparator: function(idx)         { return false; },  
isSorted: function()               { return false; },  
isEditable: function(idx, column)  { return false; },  
getLevel: function(idx) {  
	return 0;  
},  
toggleOpenState: function(idx) { },  
getImageSrc: function(idx, column) {	return null; },
getProgressMode : function(idx,column) {},
getCellValue: function(idx, column) {},
cycleHeader: function(col, elem) {},
selectionChanged: function() {},
cycleCell: function(idx, column) {},
performAction: function(action) {},
performActionOnCell: function(action, index, column) {},
getRowProperties: function(idx, column, prop) {},
getCellProperties: function(idx, column, prop) {},
getColumnProperties: function(column, element, prop) {},
_x_sentinel: 0
};


window.addEventListener("load", function(e) { 
    words_view.init();
    document.getElementById('wordstree').view = words_view;
}, false);

